package com.attendance.www.hr.controller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.CorporationBean;
import com.attendance.www.hr.beans.UserEmployeeBean;
import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.entitys.Corporation;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.ICorporationService;
import com.attendance.www.hr.service.IDepartmentService;
import com.attendance.www.hr.service.IEmployeeCorporationService;
import com.zhangkongli.frame.core.ResultMap;
/**
 * 公司管理
 * @version 2017/10/19-2017/10/19
 * @author 徐伟
 *
 */
@Controller
@RequestMapping("/corporation")
public class CorporationController {

	@Resource
	private ICorporationService CorporationService;
	@Resource
	private IEmployeeCorporationService EmployeeCorporationService;
	@Resource
	private IDepartmentService departmentService;
	
	/**
	 * 添加公司信息			
	 * @param request
	 * @param CorporationBean
	 * @return
	 */
	@RequestMapping(value = "/saveCorporation.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object saveCorporation(HttpServletRequest request, @RequestBody CorporationBean corporationBean) {
		if (corporationBean != null) {
			if (StringUtils.isBlank(corporationBean.getCorporationCode()) 
					|| StringUtils.isBlank(corporationBean.getCorporationName())
					) {
				throw new ResultFaultException("0_00001");
			}
			if (CorporationService.getCorporationByCorporation(corporationBean)) {
				throw new ResultFaultException("0_00010");
			} else {
				if (CorporationService.saveCorporation(corporationBean)) {
					return ResultMap.success();
				} else {
					throw new ResultFaultException("0_00007");
				}
			}
		}
		throw new ResultFaultException("0_00001");
	}
	/**
	 * 获取公司信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getCorporationList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getCorporationList(HttpServletRequest request, @RequestBody CorporationBean corporationBean) {
		//fromDate根据时间查询
		Date fromDate=corporationBean.getFromDate();
		if (fromDate==null) {
			fromDate= new Date();
		}
		return ResultMap.success(CorporationService.getCorporationList(corporationBean));
	}
	/**
	 * 修改公司信息		
	 * @param request
	 * @param CorporationBean
	 * @return
	 */
	@RequestMapping(value = "/updateCorporation.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object updateCorporationList(HttpServletRequest request, @RequestBody CorporationBean corporationBean) {
		if (corporationBean != null) {
			if (StringUtils.isBlank(corporationBean.getCorporationCode()) 
					|| StringUtils.isBlank(corporationBean.getCorporationName())
					) {
				throw new ResultFaultException("0_00001");
			}
			if (CorporationService.getCorporationByCorporation(corporationBean)&&
					!CorporationService.getCorporationByCorporationId(corporationBean.getCorporationId()).getCorporationCode().equals(corporationBean.getCorporationCode())) {
				throw new ResultFaultException("0_00010");
			}
			Corporation Corporation = CorporationService.getCorporationByCorporationId(corporationBean.getCorporationId());
			if (Corporation != null) {
				if (Corporation.getUpdateTime().compareTo(corporationBean.getUpdateTime()) == 0) {
					if (CorporationService.updateCorporation(corporationBean)) {
						return ResultMap.success();
					} else {
						throw new ResultFaultException("0_00007");
					}
				} else {
					throw new ResultFaultException("0_00011");
				}
			} else {
				throw new ResultFaultException("0_00012");
			}
		}
		throw new ResultFaultException("0_00001");
	}
	/**
	 * 删除公司信息			
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deleteCorporation.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object deleteCorporation(HttpServletRequest request) {
		String corporationIdString = request.getParameter("corporationId");
		if (StringUtils.isBlank(corporationIdString)) {
			throw new ResultFaultException("0_00001");
		}
		Integer corporationId = Integer.valueOf(corporationIdString);
		if(EmployeeCorporationService.getEmployeeCorporationListByCorporationId(corporationId).size()>0){
			throw new ResultFaultException("0_00015");
		}
		if(departmentService.getDepartmentByCorporationId(corporationId).size()>0) {
			throw new ResultFaultException("0_00025");
		}
		
		return ResultMap.success(CorporationService.deleteCorporation(corporationId));
	}
	
	/**
	 * 根据人查其被分配的HR权限公司
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getCorporationPermitList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getCorporationPermitList(HttpServletRequest request) {
		UserEmployeeBean userEmployee = (UserEmployeeBean) request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if (userEmployee == null) {
			throw new ResultFaultException("1_00001");
		}
		CorporationBean corporationBean=new CorporationBean();
		corporationBean.setEmployeeId(userEmployee.getEmployeeId());
		return ResultMap.success(CorporationService.getCorporationPermitList(corporationBean));
	}

}
